iT邦幫忙

2024 iThome 鐵人賽

DAY 10
0
佛心分享-刷題不只是刷題

30 天克服前端面試系列 第 10

Day 10 - 請描述 JavaScript 中 null, undefined 和 undeclared 的差異為何?

  • 分享至 

  • xImage
  •  

null

  • 變數被宣告並且被指定爲有意義的空值 null,通常用來明確地表示「沒有值」或「空物件」的狀態。null 的型別是物件。
  • 型別: null 的型別是物件。
  • 使用場景:當你明確地想表示某個變數不應該有任何值時,可以將其設為 null
let obj = null;
console.log(typeof obj); // "object"

undefined

  • 變數被宣告已經被宣告但是並沒有指派一個值,當 JavaScript 初始化變數或找不到某個物件的屬性時,會自動給它賦值為 undefined
  • 型別: undefined
  • 使用場景:當沒有給變數賦值,或者函數沒有返回值時,JavaScript 會自動給它 undefined

在鬆散的比較中, null == undefined,但在嚴格比較中 === 它們是不相等的。

console.log(null == undefined); // true
console.log(null === undefined); // false
let x;
console.log(x); // undefined
console.log(typeof x); // "undefined"

undeclared

  • undeclared 就是從未被宣告過的變數。如果在嚴格模式下嘗試使用未宣告的變數,會拋出 ReferenceError 錯誤。
  • 使用場景:undeclared 通常是由於變數使用前未宣告造成的錯誤,這在嚴格模式下尤其會被檢測出來。
"use strict";

x = 1; //會在嚴格模式下拋出 ReferenceError 錯誤,因為變數沒有被宣告
console.log(x);

在非嚴格模式下,這種行為會自動在全域範圍創建變數,這可能導致意想不到的副作用。

x = 1; // 非嚴格模式下會隱式創建全域變數 x
console.log(x); // 1


實例練習 1

Type Utilities
In this question, we will implement the following utility functions to determine the types of primitive values.

  • isBoolean(value): Return true if value is a boolean, false otherwise.
  • isNumber(value): Return true if value is a number, false otherwise. Note that NaN is considered a number.
  • isNull(value): Return true if value is null, false otherwise.
  • isString(value): Return true if value is a String, else false.
  • isSymbol(value): Return true if value is a Symbol primitive, else false.
  • isUndefined(value): Return true if value is undefined, else false.

解題 1

export function isBoolean(value) {
  return typeof value === "boolean";
}

export function isNumber(value) {
  return typeof value === "number";
}

export function isNull(value) {
  return value === null;
}

export function isString(value) {
  return typeof value === "string";
}

export function isSymbol(value) {
  return typeof value === "symbol";
}

export function isUndefined(value) {
  return typeof value === "undefined";
}

實例練習 2

9. null and undefined

// This is a JavaScript Quiz from BFE.dev

console.log(JSON.stringify([1, 2, null, 3]));
console.log(JSON.stringify([1, 2, undefined, 3]));
console.log(null === undefined);
console.log(null == undefined);
console.log(null == 0);
console.log(null < 0);
console.log(null > 0);
console.log(null <= 0);
console.log(null >= 0);
console.log(undefined == 0);
console.log(undefined < 0);
console.log(undefined > 0);
console.log(undefined <= 0);
console.log(undefined >= 0);

解題 2

// This is a JavaScript Quiz from BFE.dev

console.log(JSON.stringify([1, 2, null, 3])); //"[1,2,null,3]"
console.log(JSON.stringify([1, 2, undefined, 3])); //"[1,2,null,3]",undefined 被轉換為 null,因為 JSON 不支持 undefined 作為有效值
console.log(null === undefined); //false
console.log(null == undefined); //true
console.log(null == 0); //false
console.log(null < 0); //false
console.log(null > 0); //false
console.log(null <= 0); //true,null 會被轉換為 0
console.log(null >= 0); //true,null 會被轉換為 0
console.log(undefined == 0); //false,undefined 會被轉換為 NaN
console.log(undefined < 0); //false,undefined 會被轉換為 NaN
console.log(undefined > 0); //false,undefined 會被轉換為 NaN
console.log(undefined <= 0); //false,undefined 會被轉換為 NaN
console.log(undefined >= 0); //false,undefined 會被轉換為 NaN

本文同步於此


上一篇
Day 9 - 請描述 <script>, <script async>和 <script defer> 的差異為何?
下一篇
Day 11 - 請說明 JavaScript 中.call 和 .apply 的差異為何?
系列文
30 天克服前端面試16
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言